package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.dto.ScoreDTO;
import com.rabbiter.oes.entity.Score;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.ScoreServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "成绩")
@RestController
public class ScoreController {
    @Autowired
    private ScoreServiceImpl scoreService;

    @ApiOperation(value = "查询所有学生成绩", notes = "查询所有学生成绩", httpMethod = "GET")
    @GetMapping("/scores")
    public R<List<Score>> findAll() {
        List<Score> res = scoreService.findAll();
        return R.<List<Score>>ok("查询所有学生成绩").data(res);
    }

    @ApiOperation(value = "根据学生ID查询成绩信息(分页)", notes = "根据学生ID查询成绩信息(分页)", httpMethod = "GET")
    @GetMapping("/score/{page}/{size}/{studentId}")
    public R<IPage<Score>> findById(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("studentId") Integer studentId) {
        Page<Score> scorePage = new Page<>(page, size);
        IPage<Score> res = scoreService.findByStudentId(scorePage, studentId);
        return R.<IPage<Score>>ok("根据ID查询成绩").data(res);
    }

    @ApiOperation(value = "根据学生ID查询成绩信息(不分页)", notes = "根据学生ID查询成绩信息(不分页)", httpMethod = "GET")
    @GetMapping("/score/{studentId}")
    public R<List<Score>> findById(@PathVariable("studentId") Integer studentId) {
        List<Score> res = scoreService.findById(studentId);
        if (!res.isEmpty()) {
            return R.<List<Score>>ok().data(res);
        } else {
            return R.logicError("ID不存在");
        }
    }

    @ApiOperation(value = "添加成绩信息", notes = "添加成绩信息", httpMethod = "POST")
    @PostMapping("/score")
    public R add(@RequestBody ScoreDTO dto) {
        int res = scoreService.add(dto);
        if (res == 0) {
            return R.logicError("成绩添加失败");
        } else {
            return R.ok("成绩添加成功");
        }
    }

    @ApiOperation(value = "更具考试编号查询成绩信息", notes = "更具考试编号查询成绩信息", httpMethod = "GET")
    @GetMapping("/scores/{examCode}")
    public R<List<Score>> findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<Score> scores = scoreService.findByExamCode(examCode);
        return R.<List<Score>>ok("查询成功").data(scores);
    }

    @ApiOperation(value = "获取考试记录信息", notes = "获取考试记录信息", httpMethod = "GET")
    @GetMapping("/scoresHistory/{scoreId}")
    public R<Map<String,Object>> scoresHistory(@PathVariable("scoreId") Integer scoreId) {
        Map<String, Object> res = scoreService.scoresHistory(scoreId);
        return R.<Map<String,Object>>ok("查询成功").data(res);
    }

}
